package cn.wind.xboot.module.web.controller.common;

import cn.hutool.http.HttpUtil;
import cn.wind.xboot.core.res.ApiResult;
import cn.wind.xboot.core.utils.Assert;
import cn.wind.xboot.logic.pf.entity.PfSms;
import cn.wind.xboot.logic.pf.service.IPfSmsService;
import cn.wind.xboot.framework.domain.PageVo;
import cn.wind.xboot.module.web.vo.pf.PfSmsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @author xukk
 */
@Api(value = "/SmsParam", tags = "短信模块")
@Slf4j
@Validated
@RestController
@RequestMapping("sms")
public class SmsController {
    @Autowired
    private IPfSmsService pfSmsService;
    @Autowired
    protected DozerBeanMapper beanMapper;
/*    @ApiOperation(value = "推送", notes ="短信推送",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @PostMapping("send")
    @SuppressWarnings("unchecked")
    public ApiResult sendSMS(@RequestParam String mobiles, @RequestParam Long templateId, HttpServletRequest request) {
        ApiResult apiResponse = ApiResult.Custom();
        PfSms pfSms =pfSmsService.getById(templateId);
        Assert.checkNotNull(pfSms, "模板ID无效");
        smsManage.build(mobiles,pfSms,false, HttpUtil.getClientIP(request));
        return  apiResponse.success();
    }*/
    @ApiOperation(value = "余额查询", notes ="余额查询",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @GetMapping("queryBalance")
    @SuppressWarnings("unchecked")
    public ApiResult queryBalance() {
            return ApiResult.Custom().success();
    }
    @ApiOperation(value = "获取模板列表", notes = "获取模板列表", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id_*_*", value = "资源ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "title_*_*", value = "资源编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type_*_*", value = "标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "content_*_*", value = "url地址", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isDeleted_*_*", value = "排序", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页大小", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序", dataType = "String[]", paramType = "query"),
    })
    @GetMapping("template")
    @SuppressWarnings("unchecked")
    public ApiResult getTemplates(@ModelAttribute Map<String,Object> map,
                                  @ModelAttribute PageVo pageVo) {
        QueryWrapper<PfSms> ew=new QueryWrapper<>();
        IPage page=pfSmsService.page(pageVo.init(),ew);
        return ApiResult.Custom().success().add(page);
    }
    @ApiOperation(value = "更新模板", notes = "根据url的id来指定更新模板", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @PutMapping("template")
    @SuppressWarnings("unchecked")
    @SneakyThrows
    public ApiResult updateTemplate(@ApiParam(hidden = true) PfSmsVo smsVo) {
        PfSms pfSms = pfSmsService.getById(smsVo.getId());
        Assert.checkNotNull(pfSms, "模板ID无效");
        PfSms pfSms1=beanMapper.map(smsVo,PfSms.class);
        pfSmsService.updateById(pfSms1);
        return ApiResult.Custom().success();
    }
    @ApiOperation(value = "删除模板", notes = "根据url的id来指定删除指定模板", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @DeleteMapping("template/{id}")
    @SuppressWarnings("unchecked")
    public ApiResult deleteTemplate(@PathVariable(name="id") Integer templateId) {
        PfSms pfSms = pfSmsService.getById(templateId);
        Assert.checkNotNull(pfSms, "模板ID无效");
        pfSmsService.removeById(templateId);
        return ApiResult.Custom().success();
    }
    @ApiOperation(value = "创建推送模板", notes = "根据title,type,content创建推送模板", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @PostMapping("template")
    @SuppressWarnings("unchecked")
    public ApiResult addTemplate(@ModelAttribute PfSmsVo smsVo) {
        PfSms pfSms=beanMapper.map(smsVo,PfSms.class);
        pfSmsService.save(pfSms);
        return ApiResult.Custom().success();
    }
}
